﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace MvcWeb.Controllers
{
    public class CategoryController : BaseController
    {
        //
        // GET: /Category/

        public ActionResult Index(int pageindex = 1, int pageSize = 20)
        {
            var iquery = db.AssetCategory.AsQueryable();

            iquery = iquery.OrderBy(m => m.CategoryID);

            var list = iquery.ToPagedList(pageindex, pageSize);

            var dict = db.AssetCategory.ToList().ToDictionary(a => a.CategoryID, b => b.CategoryName);
            dict.Add(-1, "");
            ViewData["parentCategoryDic"] = dict;

            return View(list);
        }

        public ActionResult Edit(int cid = -1)
        {
            EAM.Model.AssetCategory model = null;
            if (cid == -1)
            {
                model = new EAM.Model.AssetCategory();
            }
            else
            {
                model = db.AssetCategory.Find(cid);
            }

            var categoryChooseTypeList = MvcWeb.EAMEnum.EAMEnumExt.ToDictory<MvcWeb.EAMEnum.CategoryChooseType>();

            ViewData["CategoryChooseTypeList"] = categoryChooseTypeList;

            var dict = db.AssetCategory.Where(m => m.CategoryParentID == -1 && m.CategoryIsShow == true).ToList().ToDictionary(a => a.CategoryID, b => b.CategoryName);

            ViewData["CategoryParentIDList"] = dict;

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int cid, EAM.Model.AssetCategory viewModel)
        {
            var user = System.Web.Security.Membership.GetUser();
            var guid = (Guid)user.ProviderUserKey;

            if (cid == -1)
            {
                //添加关联信息
                EAM.Model.AssetCategory model = new EAM.Model.AssetCategory()
                {
                    CategoryName = viewModel.CategoryName,
                    CategoryKeywords = viewModel.CategoryKeywords,
                    CategoryDepriciationRate = viewModel.CategoryDepriciationRate,
                    CategoryIsShow = viewModel.CategoryIsShow,
                    CategoryMaintenanCycle = viewModel.CategoryMaintenanCycle,
                    CategoryMeasure_Unit = viewModel.CategoryMeasure_Unit,
                    CategoryMonthDepriciationRate = viewModel.CategoryMonthDepriciationRate,
                    CategoryNumCount = viewModel.CategoryNumCount,
                    CategoryShowNumCount = viewModel.CategoryShowNumCount,
                    CategorySort_Order = viewModel.CategorySort_Order,
                    CategoryUsedYear = viewModel.CategoryUsedYear,
                    CategoryParentID = viewModel.CategoryParentID,
                    CategoryDescript = viewModel.CategoryDescript,
                    CategoryLabel = viewModel.CategoryLabel,
                    CategoryChooseType = viewModel.CategoryChooseType,
                    CategoryChooseValue = viewModel.CategoryChooseValue != null ? viewModel.CategoryChooseValue.Replace("，", ",") : "",
                    LastUpdateMember = guid,
                    LastUpdateTime = DateTime.Now
                };
                db.AssetCategory.Add(model);
            }
            else
            {
                var model = db.AssetCategory.Find(cid);
                db.AssetCategory.Attach(model);

                model.CategoryName = viewModel.CategoryName;
                model.CategoryKeywords = viewModel.CategoryKeywords;
                model.CategoryDepriciationRate = viewModel.CategoryDepriciationRate;
                model.CategoryIsShow = viewModel.CategoryIsShow;
                model.CategoryMaintenanCycle = viewModel.CategoryMaintenanCycle;
                model.CategoryMeasure_Unit = viewModel.CategoryMeasure_Unit;
                model.CategoryMonthDepriciationRate = viewModel.CategoryMonthDepriciationRate;
                //model.CategoryNumCount = viewModel.CategoryNumCount;
                model.CategorySort_Order = viewModel.CategorySort_Order;
                model.CategoryUsedYear = viewModel.CategoryUsedYear;
                model.CategoryParentID = viewModel.CategoryParentID;
                model.CategoryDescript = viewModel.CategoryDescript;
                model.CategoryLabel = viewModel.CategoryLabel;
                model.CategoryChooseType = viewModel.CategoryChooseType;
                model.CategoryChooseValue = viewModel.CategoryChooseValue != null ? viewModel.CategoryChooseValue.Replace("，", ",") : "";
                model.LastUpdateMember = guid;
                model.LastUpdateTime = DateTime.Now;
            }

            db.SaveChanges();
            return RedirectToAction("Index", new { id = cid });
        }

        [HttpPost]
        public ActionResult del(int id)
        {
            //资产中所有的删除都不允许
            //EAM.Model.AssetCategory model = db.AssetCategory.Find(id);
            //db.AssetCategory.Remove(model);
            return Json(true);
        }

        [HttpPost]
        public ActionResult DeleteChoose()
        {
            //资产中所有的删除都不允许
            //string ids = Request.Form["ids"];
            //if (!string.IsNullOrWhiteSpace(ids))
            //{
            //    string[] arr = ids.Split(',');
            //    foreach (var m in arr)
            //    {
            //        var model = db.AssetCategory.Find(int.Parse(m));
            //        db.AssetCategory.Remove(model);
            //    }
            //    db.SaveChanges();
            //}
            return Json(true);
        }


    }
}
